<template>
    <div class="panel-title">
        投资列表
    </div>
    <div class="panel-body">
        <table>
            <tr>
                <td>投资编号</td>
                <td>投资名称</td>
                <td>投资总金额</td>
                <td>已投资金额</td>
                <td>剩余金额</td>
                <td>投资人数</td>
                <td>投资创建时间</td>
                <td>操作</td>
            </tr>
            <tr v-for="item in Investlist" :key="item.I_Id">
                <td> {{ item.I_Id }} </td>
                <td> {{ item.I_Name }} </td>
                <td> {{ item.I_Budget }} </td>
                <td> {{ item.I_Receive }} </td>
                <td> {{ item.I_Residue }} </td>
                <td> {{ item.I_Number }} </td>
                <td> {{ item.I_Date }} </td>
                <td>
                    <input type="button" value="修改" @click="UpInvest(item.I_Id)" />
                    <input type="button" value="删除" @click="delInvest(item.I_Id)" />
                </td>
            </tr>

            <tr>
                <td colspan="4">
                    总条数：{{ datasumcount }}
                    当前页/总页数：{{ dataindex }}/{{ datasumsize }}
                    <input type="button" value="首页" @click="DataPage(0)" />
                    <input type="button" value="上一页" @click="DataPage(-1)" />
                    <input type="button" value="下一页" @click="DataPage(1)" />
                    <input type="button" value="尾页" @click="DataPage(2)" />
                </td>
            </tr>
        </table>


    </div>
</template>


<script lang="ts" setup>
import { ref, onMounted } from "vue";
import axios from "axios";
import { useRouter } from "vue-router";

//总条数
var datasumcount = ref(0);
//总页数
var datasumsize = ref(0);
//当前页
var dataindex = ref(1);
//显示数
var datasize = ref(3);
//列表
var Investlist = ref([]);

//用户名
var uname = ref("");



//查询分页
function DataPage(o: any) {
    // alert(o)
    let pageindex = dataindex.value;
    // alert(pageindex);
    let pagesize = datasize.value;
    if (o == 0) {
        dataindex = ref(1);
        pageindex = 1;
    }
    else if (o == -1) {
        dataindex = ref(dataindex.value - 1);
        pageindex = pageindex - 1;
    }
    else if (o == 1) {
        dataindex.value = dataindex.value + 1;
        pageindex = pageindex + 1;
    }
    else if (o == 2) {
        dataindex.value = datasumsize.value;
        pageindex = datasumsize.value;
    }
    //  alert(pageindex);
    //  alert(pagesize);
    //  alert(uname.value);

    axios.post("https://localhost:44395/GetPageInvest?pageindex=" + pageindex + "&pagesize=" + pagesize + "&iname=&inumber=0", {})
        .then(function (res) {
            console.log(res.data);
            Investlist.value = res.data;
            //alert(userlist.value[0].U_Id);
        })
        .catch(function (err) {
            console.log(err);
        });

}
//查询总条数

function pagecount() {
    axios.post("https://localhost:44395/GetPageCountInvest?iname=&inumber=0")
        .then(function (res) {
            console.log(res.data);
            datasumcount.value = res.data;
            datasumsize.value = Math.ceil(datasumcount.value / datasize.value);
        })
        .catch(function (err) {
            console.log(err);
        });
}


onMounted(() => {
    //loaddata();
    pagecount();
    DataPage(0);

});


//修改方法
function UpInvest(o: any) {
    if (o == null || o == "") {
        alert('参数错误');
        return;
    }
    let r = useRouter();
    r.push({ path: '/InvestUp', query: { id: o } });
}

function delInvest(o: any) {
    if (o == null || o == "") {
        alert('参数错误');
        return;
    }

    axios.post("https://localhost:44395/DelInvest?id=" + o)
        .then(function (res) {
            console.log(res.data);
            if (res.data > 0) {
                alert('删除成功');
            }
            else {
                alert('删除失败');
            }
            DataPage(0);
        })
}

</script>